﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;

namespace RTracker.Areas.Recruitment.Models.CandidateDriveRound
{
    public class MCandidateDriveRound
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListUser"]);


        /// <summary>
        /// This method is used to create a candidate drive round in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDrive">This object contains details of the candidate drive.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateCandidateDriveRound(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
             using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_create_candidate_drive_round_procedure");
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCandidateDriveRound.OrganizationId;
                        cmd.Parameters.Add("@drive_round_name", SqlDbType.NVarChar).Value = objCCandidateDriveRound.DriveRoundName;
                        cmd.Parameters.Add("@drive_round_desc", SqlDbType.NVarChar).Value = objCCandidateDriveRound.DriveRoundDesc;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateDriveRound.CreatedBy;
                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a drive round in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the drive round.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteCandidateDriveRound(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            if (!CCommonCollection.ConnectionString.Equals(string.Empty))
            {
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_delete_candidate_drive_round_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                        cmd.Parameters.Add("@candidate_drive_round_ids", SqlDbType.NVarChar).Value = objCCandidateDriveRound.CandidateDriveRoundIds;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            }
            else
            {
                oCStatus.Success = false;
                oCStatus.StatusDetails = CAppErrorsConstants.ConStringNotFound;
            }

            return oCStatus;
        }

        public CStatus UpdateCandidateDriveRound(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            if (!CCommonCollection.ConnectionString.Equals(string.Empty))
            {
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_update_candidate_drive_round_procedure");
                        cmd.CommandType = CommandType.StoredProcedure;
                        //@drive_round_id [int] 
                        cmd.Parameters.Add("@drive_round_id", SqlDbType.Int).Value = objCCandidateDriveRound.CandidateDriveRoundId;
                        //,@drive_round_name [nvarchar] (50)
                        cmd.Parameters.Add("@drive_round_name", SqlDbType.NVarChar).Value = objCCandidateDriveRound.DriveRoundName;
                        //,@drive_round_desc [nvarchar] (500)
                        cmd.Parameters.Add("@drive_round_desc", SqlDbType.NVarChar).Value = objCCandidateDriveRound.DriveRoundDesc;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateDriveRound.CreatedBy;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                        
                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the CandidateDriveRound's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCandidateDriveRoundDetail(CAuthenticationContext objCAuthenticationContext, ref CCandidateDriveRound oCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_drive_round_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@drive_round_id", SqlDbType.Int).Value = oCCandidateDriveRound.CandidateDriveRoundId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        oCCandidateDriveRound = dt.ConvertToListFromDataTableCandidateDriveRound()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        #region Lock
        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateDriveRoundSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveRound oCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_set_candidate_drive_round_soft_lock_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@drive_round_id", SqlDbType.NVarChar).Value = oCCandidateDriveRound.CandidateDriveRoundId;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
           

            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateDriveRoundHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
           
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_set_candidate_drive_round_hard_lock_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@drive_round_id", SqlDbType.NVarChar).Value = objCCandidateDriveRound.CandidateDriveRoundId;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            

            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateDriveRoundSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_release_candidate_drive_round_soft_lock_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@drive_round_id", SqlDbType.NVarChar).Value = objCCandidateDriveRound.CandidateDriveRoundId;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            

            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateDriveRoundHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateDriveRound objCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            
                using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
                {
                    try
                    {
                        con.Open();
                        SqlCommand cmd = new SqlCommand("r_release_candidate_drive_round_hard_lock_procedure", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@drive_round_id", SqlDbType.NVarChar).Value = objCCandidateDriveRound.CandidateDriveRoundId;
                        cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                        cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                        cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                        cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                        cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                        cmd.ExecuteNonQuery();

                        if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                        {
                            oCStatus.Success = true;
                            oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                        }
                        else
                        {
                            oCStatus.Success = false;
                            oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                            //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                        }
                    }
                    catch (Exception ex)
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = ex.Message;
                    }
                }
            

            return oCStatus;
        }

        #endregion

        /// <summary>
        ///  This function is used to get complete CandidateDriveRound list
        /// </summary>
        /// <returns>List of type CCandidateDriveRound</returns>
        public GCandidateDriveRound GetCandidateDriveRoundGridList(CCandidateDriveRound objCCandidateDriveRound, CAuthenticationContext objCAuthenticationContext)
        {
            GCandidateDriveRound objGCandidateDriveRound = new GCandidateDriveRound();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCCandidateDriveRound.RowSize != 0)
            {
                nCurrentRowSize = objCCandidateDriveRound.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_drive_round_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@drive_round_id", SqlDbType.Int).Value = objCCandidateDriveRound.CandidateDriveRoundId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCandidateDriveRound.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCCandidateDriveRound.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCCandidateDriveRound.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCCandidateDriveRound.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCCandidateDriveRound.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCCandidateDriveRound.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGCandidateDriveRound.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGCandidateDriveRound.LstCandidateDriveRound = dt.ConvertToListFromDataTableCandidateDriveRound();
                        objGCandidateDriveRound.LstCandidateDriveRound[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGCandidateDriveRound.Paging.PageNumber = objGCandidateDriveRound.LstCandidateDriveRound[0].PageNumber;
                    }
                    else
                    {
                        objGCandidateDriveRound.LstCandidateDriveRound = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGCandidateDriveRound.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGCandidateDriveRound.Status.Success = false;
                        objGCandidateDriveRound.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGCandidateDriveRound.Paging.TotalRecordsCount = nTotalRecords;
                        objGCandidateDriveRound.Paging.PageSize = nCurrentRowSize;

                        objGCandidateDriveRound.Status.Success = true;
                        objGCandidateDriveRound.Status.StatusDetails = "success";
                    }



                }
                catch (Exception ex)
                {
                    objGCandidateDriveRound.Status = new CStatus();
                    objGCandidateDriveRound.Status.Success = false;
                    objGCandidateDriveRound.Status.StatusDetails = ex.Message;
                }
            }
            return objGCandidateDriveRound;
        }

        /// /// <summary>
        ///  This function is used to get complete CandidateDriveRound list working under a same organization.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateDriveRound's authentication context.</param>
        /// <param name="objCCandidateDriveRound">This object contains details of the CandidateDriveRound.</param>
        /// <param name="lstCCandidateDriveRound">"out" the list of CandidateDriveRounds</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCandidateDriveRoundList(CAuthenticationContext objCAuthenticationContext, CCandidateDriveRound objCCandidateDriveRound, out List<CCandidateDriveRound> lstobjCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            lstobjCCandidateDriveRound = new List<CCandidateDriveRound>();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_get_candidate_drive_round_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCandidateDriveRound.OrganizationId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    //cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    //cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        lstobjCCandidateDriveRound.AddRange(dt.ConvertToListFromDataTableCandidateDriveRound());
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This function is used to get complete CandidateDriveRound list with firstname and lastname only by requesting ajax layer function
        /// </summary>
        /// <param name="lstCCandidateDriveRound">"out" the list of CandidateDriveRounds</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetAllCandidateDriveRoundList(out List<CCandidateDriveRound> lstobjCCandidateDriveRound)
        {
            CStatus oCStatus = new CStatus();
            lstobjCCandidateDriveRound = new List<CCandidateDriveRound>();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_get_all_candidate_drive_round_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    //cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    //cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        lstobjCCandidateDriveRound.AddRange(dt.ConvertToListFromDataTableCandidateDriveRound());
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }
    }
    public static class MCandidateDriveRoundConverter
    {
        private static Boolean ColContain(DataTable dt, DataRow dtRow, string colname)
        {
            Boolean res = false;
            if (dt.Columns.Contains(colname) && dtRow[colname] != DBNull.Value)
            {
                res = true;
            }
            return res;
        }
        public static List<CCandidateDriveRound> ConvertToListFromDataTableCandidateDriveRound(this DataTable dt)
        {

            List<CCandidateDriveRound> lstTmp = new List<CCandidateDriveRound>();

            foreach (DataRow dtRow in dt.Rows)
            {
                CCandidateDriveRound oTmp = new CCandidateDriveRound();

                if (ColContain(dt, dtRow, "drive_round_id"))
                    oTmp.CandidateDriveRoundId = Convert.ToInt32(dtRow["drive_round_id"]);
                if (ColContain(dt, dtRow, "organization_id"))
                    oTmp.OrganizationId = Convert.ToInt32(dtRow["organization_id"]);
                if (ColContain(dt, dtRow, "drive_round_name"))
                    oTmp.DriveRoundName = Convert.ToString(dtRow["drive_round_name"]);
                if (ColContain(dt, dtRow, "drive_round_desc"))
                    oTmp.DriveRoundDesc = Convert.ToString(dtRow["drive_round_desc"]);
                if (ColContain(dt, dtRow, "drive_round_type"))
                    oTmp.DriveRoundType = Convert.ToInt32(dtRow["drive_round_type"]);
                
                if (ColContain(dt, dtRow, "is_soft_locked"))
                    oTmp.IsSoftLocked = Convert.ToBoolean(dtRow["is_soft_locked"]);
                if (ColContain(dt, dtRow, "soft_locked_by"))
                    oTmp.SoftLockedBy = Convert.ToInt32(dtRow["soft_locked_by"]);
                if (ColContain(dt, dtRow, "soft_lock_timestamp"))
                    oTmp.SoftLockTimestamp = Convert.ToDateTime(dtRow["soft_lock_timestamp"]);
                if (ColContain(dt, dtRow, "is_hard_locked"))
                    oTmp.IsHardLocked = Convert.ToBoolean(dtRow["is_hard_locked"]);
                if (ColContain(dt, dtRow, "hard_locked_by"))
                    oTmp.HardLockedBy = Convert.ToInt32(dtRow["hard_locked_by"]);
                if (ColContain(dt, dtRow, "hard_lock_timestamp"))
                    oTmp.HardLockTimestamp = Convert.ToDateTime(dtRow["hard_lock_timestamp"]);
                if (ColContain(dt, dtRow, "create_timestamp"))
                    oTmp.CreateTimestamp = Convert.ToDateTime(dtRow["create_timestamp"]);
                if (ColContain(dt, dtRow, "created_by"))
                    oTmp.CreatedBy = Convert.ToInt32(dtRow["created_by"]);
                if (ColContain(dt, dtRow, "modified_timestamp"))
                    oTmp.ModifiedTimestamp = Convert.ToDateTime(dtRow["modified_timestamp"]);
                if (ColContain(dt, dtRow, "modified_by"))
                    oTmp.ModifiedBy = Convert.ToInt32(dtRow["modified_by"]);
                if (ColContain(dt, dtRow, "delete_flag"))
                    oTmp.DeleteFlag = Convert.ToBoolean(dtRow["delete_flag"]);

                lstTmp.Add(oTmp);

            }
            return lstTmp;
        }
    }
}